package com.zhang.zxx.boot.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.annotation.AutoTrans;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zhang.zxx.boot.common.base.Response;
import com.zhang.zxx.boot.common.core.aop.MvcLog;
import com.zhang.zxx.boot.common.core.event.LogEvent;
import com.zhang.zxx.boot.common.core.listener.MyEventListener;
import com.zhang.zxx.boot.domain.dto.LoginInputDTO;
import com.zhang.zxx.boot.domain.dto.UserDTO;
import com.zhang.zxx.boot.domain.dto.UserPageDTO;
import com.zhang.zxx.boot.entity.DO.SysUserInfo;
import com.zhang.zxx.boot.service.SysUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 用户表: 接口控制层
 *
 * @author zhangxiaoxiang
 * @date 2021-11-21
 */
@Slf4j
@RestController
@Api(tags = "用户表接口管理")
@RequiredArgsConstructor
@RequestMapping("/sys-user-info")
public class SysUserInfoController {
    private final SysUserInfoService service;
    private final MyEventListener eventListener;

    @MvcLog
    @ApiOperation(value = "SysUserInfo列表接口")
    @PostMapping("/list")
    public Object list() {
        log.info("进入了SysUserInfo列表接口");
        return service.list();
    }

    @MvcLog
    @ApiOperation(value = "SysUserInfo带参列表接口")
    @PostMapping("/users")
    @AutoTrans
    public Response<List<SysUserInfo>> getUsers(@RequestBody UserDTO dto) {
        List<SysUserInfo> users = service.getUsers(dto);
        LogEvent event = LogEvent.builder(users,dto);
        eventListener.logEvent(event);
        return Response.data(users);
    }

    @MvcLog
    @ApiOperation(value = "SysUserInfo分页")
    @PostMapping("/page")
    @AutoTrans
    @ApiOperationSupport(order = 1, author = "张晓祥")
    public Response<IPage<SysUserInfo>> page(@RequestBody UserPageDTO dto) {
        return Response.data(service.sysUserInfoPage(dto));
    }

    // @MvcLog
    @ApiOperation(value = "byId")
    @PostMapping("/byId")
    @AutoTrans
    @ApiOperationSupport(order = 1, author = "张晓祥")
    public Response<SysUserInfo> byId(@RequestBody UserPageDTO dto) {
        return Response.data(service.getById(dto.getId()));
    }

    @MvcLog
    @ApiOperation(value = "用户登录接口")
    @PostMapping("/login")
    public Response<String> login(@RequestBody LoginInputDTO dto) {
        return Response.data(service.login(dto));
    }
}
